package com.sansecy.monitor.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.sansecy.monitor.R;
import com.sansecy.monitor.see.LogBean;
import com.sansecy.monitor.utils.Utils;
import com.sansecy.monitor.view.RequestView;
import com.sansecy.monitor.view.ResponseView;

/**
 * @Description TODO
 * @Date 2019-08-04 13:21
 * @Created by caoyu
 */
public class LogDetailDialog extends Dialog {
    private TextView mResponseTextView;
    private TextView mRequestTextView;
    private LogBean log;

    public LogDetailDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_log_detail);
        final ViewPager viewPager = findViewById(R.id.view_pager);
        final TextView tvRequest = findViewById(R.id.tv_request);
        final TextView tvResponse = findViewById(R.id.tv_response);
        tvRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvRequest.setTextColor(Color.RED);
                tvResponse.setTextColor(Color.BLACK);
                viewPager.setCurrentItem(0);
            }
        });
        tvResponse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvRequest.setTextColor(Color.BLACK);
                tvResponse.setTextColor(Color.RED);
                viewPager.setCurrentItem(1);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    tvRequest.setTextColor(Color.RED);
                    tvResponse.setTextColor(Color.BLACK);
                } else {
                    tvRequest.setTextColor(Color.BLACK);
                    tvResponse.setTextColor(Color.RED);
                }
            }
        });
        viewPager.setAdapter(new PagerAdapter() {
            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                RequestView requestView = new RequestView(getContext());
                requestView.bindData(log);
                container.addView(requestView);
                ResponseView responseView = new ResponseView(getContext());
                responseView.bindData(log);
                container.addView(responseView);
                if (position == 0) {
                    return requestView;
                } else {
                    return responseView;
                }
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                if (position == 0) {
                    return "request";
                } else {
                    return "response";
                }
            }
        });
        TextView tvUrl = findViewById(R.id.tv_url);
        TextView tvPath = findViewById(R.id.tv_path);
        findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        if (log != null) {
            tvUrl.setText(log.getUrl());
            tvPath.setText(log.getPath());
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window win = getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        assert win != null;
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        win.getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        //设置dialog高度
        params.height = (int) (Utils.getScreenHeight(getContext()) * 0.9);
        //设置dialog宽度
        params.width = (int) (Utils.getScreenWidth(getContext()) * 0.85);
        win.setAttributes(params);

    }

    public void setLog(LogBean log) {
        this.log = log;
    }

}
